package com.m4399.minigamedemo

import android.content.Context
import android.util.Log
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import org.json.JSONObject


object WxShareUtils {
    const val WX_APP_ID = "wx1131b46b69f840a5"
    var currentGameId = ""

    fun share(context: Context, data: String) {
        // 通过appId得到IWXAPI这个对象
        Log.d("WxShareUtils", "share: data $data")
        val wxapi = WXAPIFactory.createWXAPI(context, WX_APP_ID)
//
//        val wxTextObject = WXTextObject()
//        wxTextObject.text = "分享"
//        // 用WXWebpageObject对象初始化一个WXMediaMessage对象
//        val msg = WXMediaMessage().apply {
//            mediaObject = wxTextObject
//            title = "分享测试"
//            description = "分享一个小游戏"
//            messageExt = "100110000860"
//        }
//
//
//        // 构造一个Req
//        val req = SendMessageToWX.Req()
//        req.transaction = "text"
//        req.message = msg
//
//        req.scene = SendMessageToWX.Req.WXSceneSession
//
//        // 向微信发送请求
//        wxapi.sendReq(req)

        val jsonObject = JSONObject(data)


        val webpage = WXWebpageObject()
        webpage.webpageUrl = "http://www.baidu.com"

        Log.d("MiniSdk", "share: ${jsonObject.optString("title")}")
        val title = jsonObject.optString("title", "title")
        val message = jsonObject.optString("message", "message")
        val msg = WXMediaMessage(webpage)
        msg.title = title
        msg.messageExt = "messageExt"
        msg.description = message

        val req = SendMessageToWX.Req()
        req.message = msg

        wxapi.sendReq(req)
    }

}